﻿using Newtonsoft.Json.Linq;
using System;

namespace Jinndev {

    /// <summary>
    /// Cocos预设
    /// </summary>
    public class CocosPrefab {

        /// <summary>原始数据</summary>
        public JObject obj;

        /// <summary>根节点</summary>
        public CocosNode rootNode;

        public string Name => obj["_name"].Value<string>();

        /// <summary>
        /// 遍历所有组件
        /// </summary>
        /// <param name="action">遍历回调</param>
        public void ForEachComponent(Action<CocosComponent> action) {
            ForEachChildrenComponent(rootNode, action);
        }

        /// <summary>
        /// 递归遍历所有组件
        /// </summary>
        private void ForEachChildrenComponent(CocosNode node, Action<CocosComponent> action) {
            foreach (var com in node.components) {
                action(com);
            }
            foreach (var child in node.children) {
                ForEachChildrenComponent(child, action);
            }
        }
    }

}
